package scanner.com.dialog;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import scanner.com.activity.QRMakeActivity;
import scanner.com.scanner.R;

/**
 * Created by Administrator on 2016/7/9.
 */
public class ColorWindow extends PopupWindow {
    private Context context;
    private View dialogView, windowView, bgTab, foreTab, btnbull;
    private GridView Gs;
    private LayoutInflater inflater;
    private boolean isBackground = true;

    public ColorWindow(Context context) {
        this.context = context;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        dialogView = inflater.inflate(R.layout.dialog_color, null);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setContentView(dialogView);
        findView();
        initView();
    }

    private void findView() {
        bgTab = dialogView.findViewById(R.id.foreground_tab2);
        foreTab = dialogView.findViewById(R.id.foreground_tab);
        windowView = dialogView.findViewById(R.id.window_view);
        Gs = (GridView) dialogView.findViewById(R.id.palette);
        btnbull = dialogView.findViewById(R.id.btn_qr);
    }

    private void initView() {
        bgTab.setOnClickListener(onClickListener);
        foreTab.setOnClickListener(onClickListener);
        btnbull.setOnClickListener(onClickListener);
        int[] colorArray = {
                R.color.palette_color_11, R.color.palette_color_12, R.color.palette_color_13, R.color.palette_color_14, R.color.palette_color_15,
                R.color.palette_color_21, R.color.palette_color_22, R.color.palette_color_23, R.color.palette_color_24, R.color.palette_color_25,
                R.color.palette_color_31, R.color.palette_color_32, R.color.palette_color_33, R.color.palette_color_34, R.color.palette_color_35,
                R.color.palette_color_41, R.color.palette_color_42, R.color.palette_color_43, R.color.palette_color_44, R.color.palette_color_45,
                R.color.palette_color_51, R.color.palette_color_52, R.color.palette_color_53, R.color.palette_color_54, R.color.palette_color_55,
                R.color.palette_color_61, R.color.palette_color_62, R.color.palette_color_63, R.color.palette_color_64, R.color.palette_color_65
        };
        Gs.setAdapter(new ColorAdapter(context,colorArray));
    }

    public void show(View parent) {
        showAtLocation(parent, Gravity.BOTTOM, 0, (int) (context.getResources().getDisplayMetrics().density * 90));
    }



    protected View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.foreground_tab2:
                    isBackground=true;
                    windowView.setBackgroundResource(R.mipmap.palette_page_left);
                    break;

                case R.id.foreground_tab:
                    isBackground=false;
                    windowView.setBackgroundResource(R.mipmap.palette_page_right);
                    break;
                case R.id.btn_qr:
                    dismiss();
                    break;

            }
        }
    };
    private  class  ColorAdapter extends BaseAdapter{
        private  Context context;
        private  int[] colorArray;
        private ImageView bgPreIV,fgPreIV;
        public  ColorAdapter (Context context,int[] colorArray){
            this.colorArray=colorArray;
            this.context=context;
        }

        @Override
        public int getCount() {
            return  colorArray.length;
        }

        @Override
        public Integer getItem(int position) {
            return colorArray[position];
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView=null;
            if (convertView==null){
                convertView=View.inflate(context,R.layout.img_activity,null);
                imageView= (ImageView) convertView;
                convertView.setTag(imageView);
            }else{ imageView =(ImageView) convertView.getTag();}
            imageView.setImageResource( getItem(position));
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ImageView iv=((ImageView)v);
                    int color= ((ColorDrawable)iv.getDrawable()).getColor();
                    if(isBackground){
                        bgPreIV = iv;
                        if(onColorChangedListener != null) onColorChangedListener.onBgColorChanged(color);
                    } else {
                        fgPreIV = iv;
                        if(onColorChangedListener != null) onColorChangedListener.onFgColorChanged(color);
                    }

                }
            });
            return convertView;
        }
    }

    private OnColorChangedListener onColorChangedListener;

    public void setOnColorChangedListener(OnColorChangedListener onColorChangedListener) {
        this.onColorChangedListener = onColorChangedListener;
    }

    public interface OnColorChangedListener {
        void onBgColorChanged(int color);
        void onFgColorChanged(int color);
    }
}
